<?php

namespace app\api\controller;

use app\api\model\WxappPage;
use app\store\model\Introduce;

/**
 * 页面控制器
 * Class Index
 * @package app\api\controller
 */
class Page extends Controller
{   
    /** 
     * 页面数据
     * @param null $page_id
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index($page_id = null)
    {
        
        // 页面元素
        $data = WxappPage::getPageData($this->getUser(false), $page_id);
        
        return $this->renderSuccess($data);
    }

    /**
     *  主页横幅展示数据
     */
    public function banner($page_id = null)
    {
        $data = Introduce::with('image')
                ->where('status','=',1)
                ->where('is_banner','=',1)
                ->where('is_delete','=',0)
                ->order('sort','desc')
                ->select();
        if(!$data || $data->isEmpty()){
            return $this->renderError('无数据');
        }
        return $this->renderSuccess($data);
    }

    /**
     *  主页热门推荐
     */
    public function recommend($page_id = null)
    {
        $data = Introduce::with('image')
                ->where('status','=',1)
                ->where('is_banner','=',0)
                ->where('is_delete','=',0)
                ->order(['recommend'=>'desc','sort' => 'asc'])
                ->select();
        if(!$data || $data->isEmpty()){
            return $this->renderError('无数据');
        }
        return $this->renderSuccess($data);
    }

    /**
     * 首页diy数据 (即将废弃)
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function home()
    {
        // 页面元素
        $data = WxappPage::getPageData($this->getUser(false));
        return $this->renderSuccess($data);
    }

    /**
     * 自定义页数据 (即将废弃)
     * @param $page_id
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function custom($page_id)
    {
        // 页面元素
        $data = WxappPage::getPageData($this->getUser(false), $page_id);
        return $this->renderSuccess($data);
    }

}
